﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ribbon.Web.Helper;

namespace Ribbon.Web
{
    [ParseChildren(true)]
    public class RibbonGroup : WebControl
    {
        public RibbonGroup()
        {
            this.Columns = new ControlCollection<RibbonGroup, RibbonColumn>(this);
        }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Ribbon.Web.Helper.ControlCollection<RibbonGroup, RibbonColumn> Columns { private set; get; }

        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Li);
        }
        public override void RenderEndTag(HtmlTextWriter writer)
        {
            writer.RenderEndTag();

            writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingLeft, "2px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.PaddingRight, "2px");
            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute(HtmlTextWriterAttribute.Class, "RibbonSeparatorV");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        protected override void CreateChildControls()
        {
            Table table = new Table();
            table.CellSpacing = 0;
            table.CellPadding = 0;
            table.Style.Add(HtmlTextWriterStyle.FontSize,"8pt");

            //-------------------------------------------
            TableRow trColumns = new TableRow();
            foreach(RibbonColumn column in this.Columns)
            {
                TableCell tdRibbonColumn = new TableCell();
                tdRibbonColumn.VerticalAlign = VerticalAlign.Top;

                tdRibbonColumn.Controls.Add(column);

                trColumns.Cells.Add(tdRibbonColumn);
            }
            table.Rows.Add(trColumns);
            //-----------------------------------------
            var trTitle = new TableRow();
            var tdTitle = new TableCell();
            tdTitle.ColumnSpan = trColumns.Cells.Count;
            tdTitle.Style.Add(HtmlTextWriterStyle.Height, "15px");
            tdTitle.Style.Add(HtmlTextWriterStyle.TextAlign, "center");
            tdTitle.Style.Add(HtmlTextWriterStyle.Color, "#888888");
            tdTitle.Style.Add(HtmlTextWriterStyle.Height, "15px");
            tdTitle.VerticalAlign = VerticalAlign.Bottom;
            tdTitle.Text = this.Text;
            trTitle.Cells.Add(tdTitle);
            table.Rows.Add(trTitle);
            //------------------------------------------------
            this.Controls.Add(table);
        }
  
        public string Text
        {
            get { return this.ViewState["Text"] as string ?? string.Empty; }
            set { this.ViewState["Text"] = value; }
        }
        public HorizontalAlign TextAlign
        {
            set
            {
                this.ViewState["TextAlign"] = value;
            }
            get
            {
                var obj = this.ViewState["TextAlign"];
                if (obj == null)
                    return HorizontalAlign.NotSet;
                return (HorizontalAlign) obj;
            }
        }
    }
}